package dao.jdbc;

import java.util.Arrays;

import domain.jdbc.PageBean;

public abstract class PageDao<T> extends AbstractDao<T> {
	/**
	 * *获取指定页码的数据
	 * @param page  要查询页码的信息
	 * @param sql   select * from users where age>=? 
	 * @param param 18
	 */
	public PageBean<T> searchPage(PageBean<T> page,String sql,String where,Object...param){
		if(param==null){
			param=new Object[0];
		}
		where=where==null?" ":" "+where;
		String tName=page.gettName();
		if(tName==null || "".equals(tName)){
			throw new RuntimeException();
		}
		//pageBean init
		StringBuffer countSql=new StringBuffer("select count(*) from "+tName+where);
		Integer count=Integer.valueOf(this.findFirstField(countSql.toString(), param).toString());
		page.setRowCount(count);
		
		sql+=where+" limit ?,?";
		Object[] o=Arrays.copyOf(param, param.length+2);
		o[o.length-1]=page.getPageSize();
		o[o.length-2]=(page.getNowPage()-1)*page.getPageSize();
		//(当前页数-1)*每页个数
		page.setItems(this.find(sql.toString(),o));
		return page;
	}
}
